package com.elock.intf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.entity.Car;
import com.elock.entity.CarSaleOrg;
import com.elock.entity.CompanyInfo;
import com.elock.entity.SaleOrg;
import com.elock.intf.dao.IntfCarDao;
import com.elock.intf.dao.IntfSaleOrgDao;
import com.elock.intf.service.IntfCarService;
import com.elock.service.CarSaleOrgService;
import com.elock.service.CompanyInfoService;
import com.elock.service.SaleOrgBaseService;

/**
 * 车辆
 * @author 仁德
 */
@Service("intfCarServiceImpl")
public class IntfCarServiceImpl extends BaseServiceImpl<Car> implements IntfCarService {

	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "carSaleOrgServiceImpl")
	private CarSaleOrgService carSaleOrgService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;
	@Resource(name = "intfCarDao")
	private IntfCarDao intfCarDao;
	@Resource(name = "intfSaleOrgDao")
	private IntfSaleOrgDao intfSaleOrgDao;

	public void saveUpdate(String companyCode, Car car, String saleOrgSn) {

		if (ConvertUtil.isEmpty(companyCode)) {
			ExceptionUtil.throwServiceException("请传入企业编号");
		}
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("company_code", companyCode));
		CompanyInfo companyInfo = companyInfoService.find(filters);
		if (companyInfo == null) {
			ExceptionUtil.throwServiceException("不存在企业编号为[" + companyCode + "]的企业");
		}
		String carNumber = car.getCarNumber();
		if (ConvertUtil.isEmpty(carNumber)) {
			ExceptionUtil.throwServiceException("请传入车牌号");
		}

		Car pCar = intfCarDao.findByCarNumber(carNumber);
		if (pCar == null) {

			car.setCompanyInfoId(companyInfo.getId());
			this.save(car);
		}
		else {
			if (!ConvertUtil.isEmpty(car.getCarType())) {
				pCar.setCarType(car.getCarType());
			}
			if (!ConvertUtil.isEmpty(car.getRemark())) {
				pCar.setRemark(car.getRemark());
			}
			this.update(pCar);
		}

		//车辆机构关联
		SaleOrg saleOrg = intfSaleOrgDao.findBySaleOrgSn(saleOrgSn);
		saveCarSaleOrg(saleOrg == null ? null : saleOrg.getId(), car);
	}

	/**
	 * 保存车辆机构关联
	 */
	public void saveCarSaleOrg(Long saleOrgId, Car car) {

		SaleOrg saleOrg = saleOrgBaseService.find(saleOrgId);
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("car", car));
		List<CarSaleOrg> carSaleOrgs = carSaleOrgService.findList(null, filters, null);
		CarSaleOrg carSaleOrg = null;
		for (CarSaleOrg org : carSaleOrgs) {
			if (saleOrg != null && org.getSaleOrg().equals(saleOrg)) {
				carSaleOrg = org;
			}
			else {
				carSaleOrgService.delete(org);
			}
		}
		if (carSaleOrg == null && saleOrg != null) {
			carSaleOrg = new CarSaleOrg();
			carSaleOrg.setSaleOrg(saleOrg);
			carSaleOrg.setCar(car);
			carSaleOrgService.save(carSaleOrg);
		}
	}

	/**
	 * 保存锁车辆关联
	 */
//	public void saveElockCar(Long[] elockId, Integer[] berth, String[] position,
//			Car car) {
//
//		List<Filter> filters = new ArrayList<Filter>();
//		filters.add(Filter.eq("carId", car.getId()));
//		List<ElockCar> elockCars = elockCarService.findList(null, filters, null);
//		List<Long> oElockIds = new ArrayList<Long>();
//		for (ElockCar elockCar : elockCars) {
//			oElockIds.add(elockCar.getElockId());
//		}
//
//		List<Long> nElockIds = new ArrayList<Long>();
//		if (elockId != null && elockId.length > 0) {
//			for (Long nElockId : elockId) {
//				nElockIds.add(nElockId);
//			}
//		}
//
//		for (int i = 0; i < nElockIds.size(); i++) {
//			Long nElockId = nElockIds.get(i);
//			if (!oElockIds.contains(nElockId)) {
//				ElockCar elockCar = new ElockCar();
//				elockCar.setCarId(car.getId());
//				elockCar.setCarNumber(car.getCarNumber());
//				elockCar.setElockId(nElockId);
//				Elock elock = elockService.find(nElockId);
//				elockCar.setElockNum(elock.getElockNum());
//				elockCar.setPosition(position[i]);
//				elockCar.setBerth(berth[i]);
//				elockCarService.save(elockCar);
//			}
//			else {
//				List<Filter> fis = new ArrayList<Filter>();
//				fis.add(Filter.eq("elockId", nElockId));
//				fis.add(Filter.eq("carId", car.getId()));
//				ElockCar elockCar = elockCarService.find(fis);
//				if (elockCar != null) {
//					elockCar.setPosition(position[i]);
//					elockCar.setBerth(berth[i]);
//					elockCarService.update(elockCar);
//				}
//			}
//		}
//
//		for (ElockCar elockCar : elockCars) {
//			if (!nElockIds.contains(elockCar.getElockId())) {
//				elockCarService.delete(elockCar);
//			}
//		}
//	}

	public Page<Map<String, Object>> findPage(Pageable pageable, Object[] args) {
		return intfCarDao.findPage(pageable, args);
	}

	public Car findByCarNumber(String carNumber) {
		return intfCarDao.findByCarNumber(carNumber);
	}
}
